﻿using Avalon.Core.Data;
using Avalon.Core.MVC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace Avalon.Core.Providers
{
    /// <summary>
    /// The action provider.
    /// </summary>
    public class ActionProvider : BaseProvider, Avalon.Core.Providers.IActionProvider
    {
        /// <summary>
        /// CTOR
        /// </summary>
        public ActionProvider() { }

        /// <summary>
        /// Create the action descriptor
        /// </summary>
        /// <returns></returns>
        public IActionDescriptor GetActionDescriptor() {
            //We need the entity descriptor because we'll have to instantiate an Action Invoker.
            IEntityDescriptor entityDescriptor = ProviderContainer.Current.EntityProvider.GetEntityDescriptor();

            //We need the renderer because this renderer supplies up the templates for looking up the view models 
            IRendererDescriptor rendererDescriptor = ProviderContainer.Current.RendererProvider.GetViewRenderer();

            //Create the action descriptor
            IActionDescriptor actionDescriptor = new ActionDescriptor();
            if (entityDescriptor.EntityType != null) {
                Type actionInvokerType = typeof(AvalonActionInvoker<>).MakeGenericType(entityDescriptor.EntityType);
                actionDescriptor.ActionInvoker = (IActionInvoker)Activator.CreateInstance(actionInvokerType);
            }
            else {
                actionDescriptor.ActionInvoker = new AvalonActionInvoker();
            }

            //retrun the action descriptor
            return actionDescriptor;
        }
    }
}
